package com.water.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author wsj
 * @version 1.0
 * @description: TODO
 * @date 2022/1/11 22:08
 */
@Component
@Aspect
public class Myaspect {

    /*切入点表达式，后面直接使用，便于统一管理*/
    @Pointcut(value = "execution(* com.water.service..*.*(..))")
    private void point(){

    }

    @Pointcut(value = "execution(* com.water.service..*.update*(..))")
    private void point1(){

    }


    @Before("point()")
    public void before(JoinPoint jp){

        Signature signature = jp.getSignature();
        String name = signature.getName();
        System.out.println("前置通知增强业务方法名："+name);
        Object[] args = jp.getArgs();
        System.out.println("方法的参数："+args.length);
        for (Object arg : args) {
            System.out.println("\t"+arg);
        }

        System.out.println("======before====");
    }

    //returning可以接受目标的返回值，并且修改！
    @AfterReturning(value = "execution(* com.water.service..*.*(..))",returning = "result")
    public void after(Object result){

        System.out.println("======after====result:"+result);
    }

    @AfterThrowing(value = "execution(* com.water.service..*.*(..))",throwing = "ex")
    public void exception(JoinPoint jp,Throwable ex){

        System.out.println("======exception====");
        System.out.println(jp.getSignature()+"方法异常，原因："+ex.getMessage());
    }

    @After(value = "execution(* com.water.service..*.*(..))")
    public void myFinally(){
        System.out.println("======myFinally====");
    }

    //相当于前置和后置的综合体
    @Around(value = "point1()")
    public Object around(ProceedingJoinPoint pr) throws Throwable {

        System.out.println("======around之前====");
        Object proceed = pr.proceed();
        System.out.println("=======around之后=====");
        return proceed;
    }

}
